package com.ynu.controller;

import com.ynu.bean.PageBean;
import com.ynu.bean.query.UserQuery;
import com.ynu.bean.entity.User;
import com.ynu.bean.vo.UserVO;
import com.ynu.constant.WeGo;
import com.ynu.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *  模块名称：
 *  模块类型：
 *  编码人：迟宽席
 *  创建时间：2023/2/24
 *  联系电话：18388068273
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/list")
    public ModelAndView list(ModelAndView mav, String key, Integer pageSize, Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }

        if (pageSize == null) {
            pageSize = WeGo.PAGE_SIZE;
        }

        UserQuery userQuery = new UserQuery();
        userQuery.setPageNum(pageNum);
        userQuery.setPageSize(pageSize);
        userQuery.setNickname(key);
        userQuery.setPhone(key);

        PageBean<UserVO> pageBean = userService.getUserByConditionWithPage(userQuery);
        mav.addObject("pageBean", pageBean);
        mav.setViewName("user/list");


        return mav;
    }

    @Resource
    private UserService userService;

    @GetMapping("/details")
    public ModelAndView getById(ModelAndView mav, Long id) {
        User user = userService.getUserById(id);
        mav.setViewName("user/details");
        mav.addObject("user", user);
        return mav;
    }

    /**
     * 打开修改用户信息页面
     * @param mav
     * @param id
     * @return
     */
    @GetMapping("/openUpdate")
    public ModelAndView poenUpdate(ModelAndView mav, Long id) {
        User user = userService.getUserById(id);
        mav.setViewName("user/update");
        mav.addObject("user", user);
        return mav;
    }

    @PostMapping("/update")
    public String update(User user) {
        userService.updateUserSensitiveById(user);
        return "redirect:/user/list";
    }

//    @PostMapping("/update")
//    public void update(User user, HttpServletResponse response) {
//        userService.updateUserSensitiveById(user);
//        //使用重定向再次打开列表页面
//        try {
//            response.sendRedirect("/wego/user/list"); //重定向
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @GetMapping("/delete")
    public String delete(Long id) {
        userService.deleteUserById(id);
        return "redirect:/user/list";
    }

    @GetMapping("/openLogin")
    public String openLogin(){
        return "frontend/login";
    }

    /**
     *
     * @param userQuery 封装了account和password
     * @return
     */
    @PostMapping("/login")
    public String login(UserQuery userQuery){
        userQuery.setPageNum(1);
        userQuery.setPageSize(1);
        PageBean<UserVO> pageBean = userService.getUserByConditionWithPage(userQuery);
        if (pageBean.getResult() == null || pageBean.getResult().size()<= 0) {
            // 登录成功
            return "frontend/login";
        }else {
            //登录失败
            return "redirect:/index";//重定向
        }
    }

//    12345679

}
